package com.webdev.newtech.controller;
import com.webdev.newtech.config.JwtConfig;
import com.webdev.newtech.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("token")
public class TokenController {
    @Resource
    private JwtConfig jwtConfig ;
    /**
     * 用户登录接口
     * @param userName
     * @param passWord
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login ( String userName,
                              String passWord){
        Map<String,Object> map  = new HashMap<>();
        //根据用户名密码获取用户的ID，省略
        String userId = 5 + "";
        String token = jwtConfig.createToken(userId) ;
        map.put("token",token) ;
        return AjaxResult.success(map) ;
    }
    /**
     * 需要 Token 验证的接口
     */
    @PostMapping("/info")
    public AjaxResult info (){
        return AjaxResult.success("info") ;
    }
    /**
     * 根据请求头的token获取userId
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(HttpServletRequest request){
        String usernameFromToken = jwtConfig.getUsernameFromToken(request.getHeader("token"));
        return AjaxResult.success(usernameFromToken) ;
    }
}